package me.zixing.java.guiandthread.ui;

import me.zixing.java.guiandthread.domain.SportableContent;
import me.zixing.java.guiandthread.domain.SportableContentEvent;
import me.zixing.java.guiandthread.domain.SportableContentListener;

import javax.swing.*;
import java.awt.*;
import java.util.EventObject;

/**
 * Created by bingh on 2017/11/15.
 */
public class ScorePanel extends JPanel implements SportableContentListener{
    private JLabel scoreLable;
    private Font font = new Font("黑体",Font.PLAIN,40);
    private volatile static boolean flag = true;
    private volatile String score = "0.0";
    public ScorePanel() {
        initUI();
        initAction();
    }

    protected void initUI(){
        this.scoreLable = new JLabel();
        scoreLable.setFont(font);
        this.scoreLable.setText(score);
        this.add(this.scoreLable);
    }

    protected void initAction(){

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        this.scoreLable.setText(score);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);
        this.scoreLable.setBounds(0,0,width,height);
    }

    @Override
    public void doWithEvent(SportableContentEvent eventObject) {
        switch(eventObject.getType()){
            case 0:break;
            case 1:
                this.score = ((SportableContent)eventObject.getSource()).getX()+"";
                repaint();
                break;
            case 2:
                this.score = flag? "WIN" : "Lost";
                flag = !flag;
                repaint();
                break;
        }
    }
}
